from selenium import webdriver
from datetime import datetime
from selenium.webdriver.support.select import Select

class BasePage:
    def __init__(self):
        self.driver=webdriver.Chrome()

    def open(self,url):
        self.driver.get(url)

    def get_element(self,locator,doc):
        print('开始定位元素%s'%str(locator))
        ele=None
        try:
            start_time=datetime.now()
            ele=self.driver.find_element(*locator)
            end_time=datetime.now()
            times=(end_time-start_time).seconds
            print('元素%s定位成功,用时%d秒' % (str(locator),times))
        except Exception as e:
            print('元素%s定位失败' % str(locator))
            self.get_screen_shoot(doc)
        return ele
    def get_screen_shoot(self,filename):
        path='./test_result/screenshot/'+filename+'png'
        self.driver.get_screenshot_as_file(path)
    def click_element(self,locator,doc):
        ele = self.get_element(locator,doc)
        if ele is None:
            print('元素定位失败，不能进行点击操作！')
        else:
            ele.click()
    def input_text(self,locator,doc,text):
        ele=self.get_element(locator,doc)
        if ele is None:
            print('元素定位失败，不能进行点击操作！')
        else:
            ele.send_keys(text)

    def swich_to_window(self,index=-1):
        handles=self.driver.window_handles
        self.driver.switch_to.window(handles[index])

    def switch_into_frame(self,locator,doc):
        ele=self.get_element(locator,doc)
        if ele is None:
            print('元素定位失败，不能进行frame切换！')
        else:
            self.driver.switch_to.frame(ele)

    def switch_outo_frame(self):
        self.driver.switch_to.default_content()

    def get_element_text(self,locator,doc):
        ele=self.get_element(locator,doc)

        if ele is None:
            print('元素定位失败，无法获取元素的文本值！')
        else:
            return ele.text

    def get_element_attribute(self,locator,doc,attribute):
        ele=self.get_element(locator,doc)
        if ele is None:
            print('元素定位失败，无法获取元素的属性值！')
        else:
            return ele.get_attribute(attribute)

    def switch_to_alert(self,action='a',text=None):
        alert=self.driver.switch_to.alert
        if action == 'a':
            alert.accept()
        elif action =='b':
            alert.dismiss()
        else:
            alert.send_keys(text)
            alert.accept()

    def select_action(self,locator,doc,index=None,value=None,visible_text=None):
        ele = self.get_element(locator,doc)
        if ele is None:
            print('元素定位失败，无法获取元素的属性值！')
        else:
            select=Select(ele)
            if index is not None:
                select.select_by_index(index)
            if value is not None:
                select.select_by_value(value)
            if visible_text is not None:
                select.select_by_visible_text(visible_text)

    def execute_js(self,js,ele=None):
        self.driver.execute_script(js,ele)

    def remove_element_attribute(self,locator,doc,attr):
        ele=self.get_element(locator,doc)
        if ele is None:
            print('元素定位失败，不能进行属性操作！')
        else:
            js='arguments[0].removeAttribute("%s")'%attr
            self.execute_js(js,ele)

    def refresh(self):
        self.driver.refresh()
    def back(self):
        self.driver.back()
    def forward(self):
        self.driver.forward()
    def quit(self):
        self.driver.quit()














